package com.crm.service.impl;

import com.crm.dao.CustomerDao;
import com.crm.dao.CustomerLossDao;
import com.crm.dao.OrderDao;
import com.crm.entity.*;
import com.crm.service.CustomerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 客户Service实现类
 *
 * @author Administrator
 */
@Service("customerService")
public class CustomerServiceImpl implements CustomerService {

    @Resource
    private CustomerDao customerDao;

    @Resource
    private CustomerLossDao customerLossDao;

    @Resource
    private OrderDao orderDao;

    @Override
    public List<Customer> find(Map<String, Object> map) {
        return customerDao.find(map);
    }

    @Override
    public Long getTotal(Map<String, Object> map) {
        return customerDao.getTotal(map);
    }

    @Override
    public int add(Customer customer) {
        return customerDao.add(customer);
    }

    @Override
    public int update(Customer customer) {
        return customerDao.update(customer);
    }

    @Override
    public int delete(Integer id) {
        return customerDao.delete(id);
    }

    @Override
    public Customer findById(Integer id) {
        return customerDao.findById(id);
    }

    @Override
    public void checkCustomerLoss() {
        List<Customer> customerList = customerDao.findLossCustomer(); // 查找流失客户
        for (Customer c : customerList) {
            CustomerLoss customerLoss = new CustomerLoss(); // 实例化客户流失实体
            customerLoss.setKhno(c.getKhno());
            customerLoss.setName(c.getName());
            customerLoss.setCusManager(c.getCusManager());
            Order order = orderDao.findLastOrderByCusId(c.getId());    // 查找指定客户最近的订单
            if (order == null) {
                customerLoss.setLastOrderTime(null);
            } else {
                customerLoss.setLastOrderTime(order.getOrderDate()); // 设置最近的下单日期
            }
            customerLossDao.add(customerLoss); // 添加到客户流失表
            c.setState(1); // 客户状态修改成1 流失状态
            customerDao.update(c);
        }
    }

    @Override
    public List<CustomerGx> findCustomerGx(Map<String, Object> map) {
        return customerDao.findCustomerGx(map);
    }

    @Override
    public Long getTotalCustomerGx(Map<String, Object> map) {
        return customerDao.getTotalCustomerGx(map);
    }

    @Override
    public List<CustomerGc> findCustomerGc() {
        return customerDao.findCustomerGc();
    }

    @Override
    public List<CustomerFw> findCustomerFw() {
        return customerDao.findCustomerFw();
    }

}
